package com.white.commonlib.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Created by ytf on 2016/11/10.
 * UI操作类
 */

public class UIHelper
{

    /**
     * @param activity
     * @return 返回屏幕的宽度
     */
    public static int getScreenWidth(Activity activity)
    {
        if (activity != null)
        {
            Display display = activity.getWindowManager().getDefaultDisplay();
            DisplayMetrics metrics = new DisplayMetrics();
            display.getMetrics(metrics);
            return metrics.widthPixels;
        }
        return -1;
    }

    /**
     * @param activity
     * @return 返回屏幕的高度
     */
    public static int getScreenHeight(Activity activity)
    {
        if (activity != null)
        {
            Display display = activity.getWindowManager().getDefaultDisplay();
            DisplayMetrics metrics = new DisplayMetrics();
            display.getMetrics(metrics);
            return metrics.heightPixels;
        }
        return -1;
    }

    //获取屏幕的宽度
    public static int getScreenWidth(Context context)
    {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

    //获取屏幕的高度
    public static int getScreenHeight(Context context)
    {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getHeight();
    }

    /***
     * dp转px
     * @param context
     * @param dp
     * @return
     */
    public static int dp2px(Context context, float dp)
    {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    //
    public static int px2dp(Context context, float px)
    {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

//    /***
//     * px转dp
//     * @param context
//     * @param px
//     * @return
//     */
//    public static float px2dp(Context context, int px)
//    {
//        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
//        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) px, metrics);
//    }
//
//    public static Resources getResources()
//    {
//        return BaseApplication.getAppContext().getResources();
//    }
//
//    public static float sp2px(Context context, int sp)
//    {
//        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
//        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, (float) sp, metrics);
//    }

    /***
     * 动态设置listview的高度
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView)
    {
        if (listView == null) return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
        {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++)
        {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1))
                + listView.getPaddingTop()
                + listView.getPaddingBottom();
        listView.setLayoutParams(params);
    }

    /***
     * 动态设置listview高度
     */
    public static void setListViewHeight(ListView listView, ListAdapter adapter, int count)
    {
        int totalHeight = 0;
        for (int i = 0; i < count; i++)
        {
            View listItem = adapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * count - 1)
                + listView.getPaddingTop()
                + listView.getPaddingBottom();
        listView.setLayoutParams(params);
    }

    /***
     * 清除父布局
     * @param v
     */
    public static void removeParent(View v)
    {
        if (v != null)
        {
            ViewParent parent = v.getParent();
            if (parent != null)
            {
                ((ViewGroup) parent).removeView(v);
            }
        }
    }

    /***
     *
     * @return 获得状态栏高度
     */
    public static int getStatusHight(Context ctx)
    {
        int identifyId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifyId > 0)
        {
            return ctx.getResources().getDimensionPixelSize(identifyId);
        }
        return 0;
    }
//
//    /***
//     * 计算ExpandableListView的高度
//     * @param listView
//     */
//    public static void calcExpandListViewHeight(ExpandableListView listView)
//    {
//        ListAdapter listAdapter = listView.getAdapter();
//        int totalHeight = 0;
//        int count = listAdapter.getCount();
//        for (int i = 0; i < count; i++)
//        {
//            View listItem = listAdapter.getView(i, null, listView);
//            listItem.measure(0, 0);
//            totalHeight += listItem.getMeasuredHeight();
//        }
//
//        ViewGroup.LayoutParams params = listView.getLayoutParams();
//        params.height = totalHeight
//                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//        listView.setLayoutParams(params);
//        listView.requestLayout();
//    }

    /***
     * 计算gridview的高度
     * @param numColumns
     * @param gridView
     * @param vSpacingHeight
     */
    public static void calGridViewWidthAndHeigh(int numColumns, GridView gridView, int vSpacingHeight)
    {

        // 获取GridView对应的Adapter
        ListAdapter listAdapter = gridView.getAdapter();
        if (listAdapter == null)
        {
            return;
        }
        int totalHeight = 0;

        if (listAdapter.getCount() <= numColumns)
        {
            View v = listAdapter.getView(0, null, gridView);
            v.measure(0, 0);
            totalHeight = v.getMeasuredHeight();
        } else
        {
            for (int i = 0, len = listAdapter.getCount(); i < len; i++)
            {
                View listItem = listAdapter.getView(i, null, gridView);
                listItem.measure(0, 0);
                int itemHeight = listItem.getMeasuredHeight();
                if ((i + 1) % numColumns == 0)
                {
                    totalHeight += itemHeight;
                    //如果是最后一排且刚好放满就不再加verticalSpacingHeight
                    if ((i + 1) != len)
                        totalHeight += vSpacingHeight;
                } else if ((i + 1) == len)
                {
                    totalHeight += itemHeight;
                }
            }
        }

        int padTop = gridView.getPaddingTop();
        int padBom = gridView.getPaddingBottom();
        totalHeight += padBom + padTop;
        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        params.height = totalHeight;
        gridView.setLayoutParams(params);
    }

    /**
     * 沉浸状态栏，支持4.4, 5.0以上可以不用沉浸，设置colorPrimaryDark颜色即可
     *
     * @param ac
     * @param color 颜色
     * @param callback 4.4回调出statuesBarView方便控制
     */
    public static void immersiveStatuesBar(Activity ac, int color, Callback callback)
    {
        Window window = ac.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(color);
        } else
        {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            ViewGroup systemContent = (ViewGroup) ac.findViewById(android.R.id.content);
            if (systemContent != null)
            {
                View statusBarView = new View(ac);
                statusBarView.setTag(statusBarView.toString());
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusHight(ac));
                statusBarView.setBackgroundColor(color);
                systemContent.addView(statusBarView, 0, lp);
                systemContent.getChildAt(0).setFitsSystemWindows(true);
                if(callback != null)
                    callback.retrieve(statusBarView);
            }
        }
    }

    public interface Callback
    {
        void retrieve(View statuesBarView);
    }

    /**
     * 亮色状态栏字体颜色，6.0以上才支持
     * @param ac
     */
    @TargetApi(23)
    public static void lightStatuesBarTxtColor(Activity ac)
    {
        ac.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }
}
